package br.com.smart.dao.generic;

import org.droidpersistence.dao.TableDefinition;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import br.com.jmmarca.model.Evento;
import br.com.jmmarca.model.EventoProduto;
import br.com.jmmarca.model.Produto;
import br.com.jmmarca.model.Usuario;
import br.com.jmmarca.model.Visitante;

public class OpenHelper extends SQLiteOpenHelper {

    public OpenHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            AbtractTableDefinition tableDefinition = null;
            
            ;
			new AbtractTableDefinition(Evento.class);
			TableDefinition.onCreate(db);
            ;
			new AbtractTableDefinition(Produto.class);
			TableDefinition.onCreate(db);
            ;
			new AbtractTableDefinition(EventoProduto.class);
			TableDefinition.onCreate(db);
            ;
			new AbtractTableDefinition(Usuario.class);
			TableDefinition.onCreate(db);
            ;
			new AbtractTableDefinition(Visitante.class);
			TableDefinition.onCreate(db);              

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            AbtractTableDefinition tableDefinition = null;
            
            ;
			new AbtractTableDefinition(Evento.class);
			TableDefinition.onUpgrade(db, oldVersion, newVersion);
            ;
			new AbtractTableDefinition(Produto.class);
			TableDefinition.onUpgrade(db, oldVersion, newVersion);
            ;
			new AbtractTableDefinition(EventoProduto.class);
			TableDefinition.onUpgrade(db, oldVersion, newVersion);
            ;
			new AbtractTableDefinition(Usuario.class);
			TableDefinition.onUpgrade(db, oldVersion, newVersion);
            ;
			new AbtractTableDefinition(Visitante.class);
			TableDefinition.onUpgrade(db, oldVersion, newVersion);  
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
